package design_pattern.producer_and_consumer;

import java.util.concurrent.BlockingQueue;

/**
 * @ClassName : design_pattern.producer_and_consumer.Producer
 * @Description : 类描述
 * Created by cyd on 2021-10-14 10:38:39
 */
public class Producer implements Runnable {

    // 阻塞队列，空和满的时候，不抛异常，只是阻塞
    private MyBlockingQueue<String> blockingQueue;

    public Producer(MyBlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100000; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                // 当blockingQueue满的时候，线程阻塞
                blockingQueue.put(Thread.currentThread().getName() + "---" + i);
                System.out.println("生产者线程，" + Thread.currentThread().getName() +
                        "生产产品：" + Thread.currentThread().getName() + "---" + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
